<?php

namespace App\Models;

use App\User;
use phpDocumentor\Reflection\Types\Self_;
use Tymon\JWTAuth\Contracts\JWTSubject;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class UsersModel extends Authenticatable implements JWTSubject
{
    use Notifiable;
    protected $table = 'users';
//    protected $fillable = ['user_account', 'password', 'user_account'];
    protected $guarded = [];
    #定义是否默认维护时间，默认是true.改为false，则以下时间相关设定无效
    public $timestamps = true;
    protected $hidden = [
        'password', 'remember_token',
    ];
    /**
     * Get the identifier that will be stored in the subject claim of the JWT.
     *
     * @return mixed
     */
    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    /**
     * Return a key value array, containing any custom claims to be added to the JWT.
     *
     * @return array
     */
    public function getJWTCustomClaims()
    {
        return [];
    }

    public function getViewTypeAttribute()
    {
        return $this->identity;
    }

    /**
     *
     * @param $fans
     */
    public function getMemberCards($fans) {
        $memberCards = array();
        foreach ($fans as $k => $v) {
            //用户会员卡
            if (!empty($v->userCards) && $v->userCards->count() > 0) {
                $data = $v->userCards->toArray();
                foreach ($data as $cards) {
                    $memberCards[$cards['id']]['number'] = !empty($memberCards[$cards['id']]['number']) ? $memberCards[$cards['id']]['number'] + 1 : 1;
                    $memberCards[$cards['id']]['user'] = !empty($memberCards[$cards['id']]) && !empty($v->userCourses) && !empty($memberCards[$cards['id']]['user']) ? array_merge($memberCards[$cards['id']]['user'], $v->userCourses->toArray()) : $v->userCourses->toArray();
                }
            }
        }
        return $memberCards;
    }

    /**
     * 员工所属校区
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function school() {
        return $this->belongsTo(SchoolModel::class, 'school_id', 'id');
    }

    public function identity() {
        return $this->hasOneThrough(
            IdentityModel::class,
            UserIdentityModel::class,
            'user_id',
            'id',
            'id',
            'identity_id'
        );
    }

    /**
     * 记录，一对多
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function dailyRecord() {
        return $this->hasMany(DailyRecordModel::class, 'user_id', 'id');
    }

    public function checkRecord() {
        return $this->hasMany(DailyRecordModel::class, 'user_review_id', 'id');
    }

    /**
     * 员工客户，一对多
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function fans() {
        return $this->hasMany(self::class, 'invitation_id', 'id');
    }

    public function customer() {
        return $this->belongsTo(self::class, 'invitation_id', 'id');
    }

    /**
     * 用户会员卡
     * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
     */
    public function userCards() {
        return $this->hasManyThrough(
            MemberCardModel::class,
            UserCardModel::class,
            'user_id',
            'id',
            'id',
            'member_card_id'
        )->where('card_status', 1);
    }

    /**
     * 用户课程
     * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
     */
    public function userCourses() {
        return $this->hasManyThrough(
            CourseModel::class,
            UserCourseModel::class,
            'user_id',
            'id',
            'id',
            'course_id'
        );
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\hasOne
     */
    public function courseStudent() {
        return $this->hasOne(CourseStudentModel::class, 'user_id', 'id');
    }




}
